package oceanlife.pathfinding;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

import oceanlife.OLSet;
import oceanlife.TestPanel;


public class PathArea {
	
	private Pnt orig = null;

	private ArrayList<RadialFace> areaBorder = new ArrayList<RadialFace>();
	
	public synchronized ArrayList<Line2> getAreaBorderLines(){
		ArrayList<Line2> lines = new ArrayList<Line2>();
		for(RadialFace rf : areaBorder){
			lines.add(rf.getBorderLine());
		}
		return lines;
	}
	private synchronized ArrayList<Pnt> getAreaBorderPnts() {
		ArrayList<Pnt> pnts = new ArrayList<Pnt>();
		for(RadialFace rf : areaBorder){
			pnts.add(rf.getPnt());
		}
		return pnts;
	}

	
	public ArrayList<RadialFace> getAreaBorder() {
		return areaBorder;
	}
	
	public synchronized void reset(Pnt orig){
		this.orig = orig;
		areaBorder.clear();
		double border = OLSet.border;
		areaBorder.add(new RadialFace(orig, new Pnt(orig.coord(0),border),new Pnt(-border,border),new Pnt(border,border)));
		areaBorder.add(new RadialFace(orig, new Pnt(orig.coord(0),-border),new Pnt(-border,-border),new Pnt(border,-border)));
		areaBorder.add(new RadialFace(orig, new Pnt(border,orig.coord(1)),new Pnt(border,-border),new Pnt(border,border)));
		areaBorder.add(new RadialFace(orig, new Pnt(-border,orig.coord(1)),new Pnt(-border,-border),new Pnt(-border,border)));
	}

	public synchronized void addAll(RadialFace[] rfs) {
		//System.out.println();
		for(int j = 0;j < rfs.length;j++){
			RadialFace borderCandidate = rfs[j];
			RadialFace[] BMCrossed = new RadialFace[]{null,null};
			Pnt[] crossPnts = new Pnt[]{null,null};
			for(int b = areaBorder.size()-1; b >= 0;b--){
				RadialFace borderMember = areaBorder.get(b);
				Pnt cross = borderCandidate.interscetionPoint(borderMember);
				if(cross != null){
					if(BMCrossed[0] == null){
						BMCrossed[0] = borderMember;
						crossPnts[0] = cross;
					} else {
						BMCrossed[1] = borderMember;
						crossPnts[1] = cross;
					}
				} else {
					Pnt crossToBM1 = borderCandidate.getBorderLine().intersectionPoint(new Line2(borderCandidate.getOrigen(),borderMember.getLineEnd1()));
					Pnt crossToBM2 = borderCandidate.getBorderLine().intersectionPoint(new Line2(borderCandidate.getOrigen(),borderMember.getLineEnd2()));
					if(crossToBM1 != null && crossToBM2 != null){
						areaBorder.remove(b);
					}
				}
			}
			if(BMCrossed[1] != null){
				for(int i = 0; i < BMCrossed.length;i++){
					Pnt crossToBM1 = borderCandidate.getBorderLine().intersectionPoint(new Line2(borderCandidate.getOrigen(),BMCrossed[i].getLineEnd1()));
					Pnt crossToBM2 = borderCandidate.getBorderLine().intersectionPoint(new Line2(borderCandidate.getOrigen(),BMCrossed[i].getLineEnd2()));
					if(crossToBM1 != null){
						BMCrossed[i].setLineEnd1(crossPnts[i]);
					} 
					if(crossToBM2 != null){
						BMCrossed[i].setLineEnd2(crossPnts[i]);
					}
				}
				borderCandidate.setLineEnd1(crossPnts[0]);
				borderCandidate.setLineEnd2(crossPnts[1]);
				areaBorder.add(borderCandidate);
			}
		}
		checkLastPoint();
	}
	
	
	//semi working
	/*private void add(RadialFace rf){
		int i = areaBorder.size()-1;
		if(areaBorder.size() == 0){
			areaBorder.add(rf);
		}
		while(i >= 0){
			Pnt cross = rf.interscetionPoint(areaBorder.get(i));
			if(cross != null){
				Pnt crossA = rf.getBorderLine().intersectionPoint(new Line2(rf.getOrigen(),areaBorder.get(i).getLineEnd1()));
				if(crossA != null){
					areaBorder.get(i).setLineEnd1(cross);
				} else {
					areaBorder.get(i).setLineEnd2(cross);
				}
				Pnt crossB = areaBorder.get(i).getBorderLine().intersectionPoint(new Line2(rf.getOrigen(),rf.getLineEnd1()));				
				if(crossB != null){
					rf.setLineEnd2(cross);
				} else {
					rf.setLineEnd1(cross);
				}
				areaBorder.add(rf);
				break;
			}
			i--;
		}
	}*/

	private void checkLastPoint() {
		// TODO Auto-generated method stub
		
	}

	public static void main(String[] args){
		int size = 500;
		final JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.getContentPane().setLayout(null);
		f.setPreferredSize(new Dimension(size,size));
		final TestPanel sp = new TestPanel();
		sp.setBounds(0, 0, size, size);
		f.add(sp);

		Random rand = new Random(6);
		Pnt orig = new Pnt(250.0,250.0);
		PathArea pa = new PathArea();
		pa.reset(orig);
		ArrayList<RadialFace> rfs = new ArrayList<RadialFace>();
		for(int i = 0; i < 20;i++){
			double x = 150+rand.nextDouble()*200;
			double y = 150+rand.nextDouble()*200;
			System.out.println("gen: "+x+" "+y);
			rfs.add(new RadialFace(orig, new Pnt(x,y)));
		}
		pa.addAll(rfs.toArray(new RadialFace[]{}));
		sp.lines.addAll(pa.getAreaBorderLines());
		sp.pnts.addAll(pa.getAreaBorderPnts());
		sp.pnts.add(orig);

		f.pack();
        f.setVisible(true);
	}
	public boolean contains(Pnt lastPoint) {
		if (orig == null){
			return false;
		}
		for(Line2 BM : this.getAreaBorderLines()){
			if(BM.intersectionPoint(new Line2(lastPoint,orig)) != null){
				return false;
			}
		}
		return true;
	}

}
